<?php
/**
*
* @package shopBB
* @copyright (c) 2008 EXreaction, Lithium Studios
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

if (!defined('IN_PHPBB'))
{
	exit;
}

include(SHOPBB_PATH . 'pages/item/functions.' . PHP_EXT);

shop::add_lang(array('post_item', array('posting', false)));

shop::header('ADD_ITEM');

setup_post_item();

$submit = (isset($_POST['submit'])) ? true : false;
$preview = (isset($_POST['preview'])) ? true : false;
$item_id = request_var('item', 0);
$error = array();

$item_data = array(
	'item_name'				=> request_var('item_name', '', true),
	'item_desc'				=> request_var('message', '', true),
	'item_approved'			=> true,
	'item_cost'				=> request_var('item_cost', 0.00),
	'item_inventory'		=> request_var('item_inventory', -1),

	'item_desc_options'		=> generate_storage_options('disable_bbcode', 'disable_smilies', 'disable_magic_url'),
	'item_desc_uid'			=> '',
	'item_desc_bitfield'	=> '',
);

if ($preview)
{
	$item_data['item_parsed_desc'] = $item_data['item_desc'];
	$enable_bbcode = $enable_magic_url = $enable_smilies = false;
	reverse_storage_options($item_data['item_desc_options'], &$enable_bbcode, &$enable_magic_url, &$enable_smilies);
	generate_text_for_storage($item_data['item_parsed_desc'], $item_data['item_desc_uid'], $item_data['item_desc_bitfield'], $item_data['item_desc_options'], $enable_bbcode, $enable_magic_url, $enable_smilies);

	$item_data['item_parsed_desc'] = generate_text_for_display($item_data['item_parsed_desc'], $item_data['item_desc_uid'], $item_data['item_desc_bitfield'], $item_data['item_desc_options']);

	$template->assign_var('S_DISPLAY_PREVIEW', true);

	$template->assign_block_vars('items', array(
		'ITEM_NAME'				=> $item_data['item_name'],
		'ITEM_PARSED_DESC'		=> $item_data['item_parsed_desc'],
		'USER_FULL'				=> get_username_string('full', $user->data['user_id'], $user->data['username'], $user->data['user_colour']),
	));
}
else if ($submit)
{
	//$error[] = 'DISABLED';

	if (!check_form_key('post_item'))
	{
		$error[] = 'INVALID_FORM_KEY';
	}

	shop::submit_item($mode, $item_data, $error);

	if (!sizeof($error))
	{
		$message = $user->lang['ADD_ITEM_SUCCESS'];

		$message .= '<br /><br />' . sprintf($user->lang['CLICK_VIEW_ITEM'], shop::build_url($item_data['item_id']));

		trigger_error($message);
	}
}

// Output the Item info
foreach ($item_data as $key => $value)
{
	$template->assign_var(strtoupper($key), $value);
}

$template->assign_vars(array(
	'S_BBCODE_CHECKED'		=> (isset($_POST['disable_bbcode'])),
	'S_SMILIES_CHECKED'		=> (isset($_POST['disable_smilies'])),
	'S_MAGIC_URL_CHECKED'	=> (isset($_POST['disable_magic_url'])),
));

$template->assign_vars(array(
	'ITEM_INVENTORY'	=> ($item_data['item_inventory'] < 0) ? '' : $item_data['item_inventory'],

	'ERROR'				=> shop::format_error($error),

	'L_POST_A'			=> $user->lang['POST_A_NEW_ITEM'],
));

shop::set_template('post_item');

?>